<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #header {
      width: 1200px;
      height: 300px;
      background: #ccc;
      margin-bottom: 20px;
    }
    #body {
      width: 1200px;
      height: 1500px;
      background-color: aqua;
    }
    #goback {
      position: absolute;
      top: 650px;
      right: 100px;
      width: 50px;
      height: 100px;
      background-color: bisque;
      color: bisque;
    }
  </style>
</head>
<body>
  <div id="header"></div>
  <div id="body"></div>
  <div id="goback">返回顶部</div>

  <script>
    var header = document.querySelector('#header')
    var body = document.querySelector('#body')
    var goback = document.querySelector('#goback')
    var bodyTop = body.offsetTop
    var fiexdTop = goback.offsetTop - bodyTop

    document.onscroll = function() {
      // 正常页面元素 滚动出去距离  scrolloffTop

      var outHeight = window.pageYOffset  // 页面滚动出去内容高度

      if(outHeight >= bodyTop) {
        goback.style.position = 'fixed'
        goback.style.top = fiexdTop + 'px'
      } else {
        goback.style.position = 'absolute'
        goback.style.top = 650 + 'px'
      }

      if(outHeight >= 800) {
        goback.style.color = '#fff'
      } else {
        goback.style.color = 'bisque'
      }
    }
  </script>
</body>
</html>